package cn.wolfcode.web.controller;

import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.service.ISeckillProductService;
import cn.wolfcode.util.UserUtil;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrderInfoController {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private IOrderInfoService orderInfoService;

    public static final HashMap<Long, Boolean> login_flg = new HashMap<>();
    @RequestMapping("/doSeckill")
    //做登录判断
    @RequireLogin
    public Result<String> doSeckill(int time, Long seckillId, HttpServletRequest request){
        //做参数校验
        if (StringUtils.isEmpty(time+"")&&StringUtils.isEmpty(String.valueOf(seckillId))){
            throw new BusinessException(SeckillCodeMsg.OPT_SECKILL);
        }
        //3.判断商品是否在活动时间内
        //4判断商品库存是否足够
        SeckillProductVo seckillProductVo = seckillProductService.queryById(time, seckillId);
        Integer stockCount = seckillProductVo.getStockCount();
        if (stockCount<=0){
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }
        //本地标识判断
        Boolean flag = login_flg.get(seckillId);
        if (flag!=null&&flag){
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }
        //访问时候进行redis库存的减少
        Long count = redisTemplate.opsForHash().increment(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(time + ""),
                seckillId+"", -1);
        //售空抛出消息异常
        if (count <=0) {
            //像本地标识中增加
            login_flg.put(seckillId,true);
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }
        //判断用户是否重复下单
        String token = request.getHeader("token");
        UserInfo userInfo = UserUtil.getUser(redisTemplate,token );
        Boolean aBoolean = redisTemplate.opsForSet().isMember(SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(time + ""),
                userInfo.getPhone() + ":" + seckillId);
        if (aBoolean) {
            throw new BusinessException(SeckillCodeMsg.REPEAT_SECKILL);
        }
//        String doseckill = orderInfoService.doSeckill(time,seckillId,userInfo.getPhone());
        OrderMessage message = new OrderMessage();
        message.setSeckillId(seckillId);
        message.setTime(time);
        message.setToken(token);
        message.setUserPhone(userInfo.getPhone());
        //发送消息
        rocketMQTemplate.sendOneWay(MQConstant.ORDER_PEDDING_TOPIC,message);
        return Result.success("正在抢购中");
    }
    @RequestMapping("/find")
    public Result<OrderInfo> find(HttpServletRequest request,String orderNo){
        //判空操作
        if (StringUtils.isEmpty(orderNo)) {
            throw new BusinessException(SeckillCodeMsg.OPT_SECKILL);
        }
        //然后进行获取order]info对象
        OrderInfo orderInfo = orderInfoService.getByOrderNo(orderNo);
        //判断用户是否是当前订单用户
        String token = request.getHeader("token");
        UserInfo user = UserUtil.getUser(redisTemplate, token);
        if (!user.getPhone().equals(orderInfo.getUserId())){
            throw new BusinessException(SeckillCodeMsg.OPT_SECKILL);
        }
        return Result.success(orderInfo);
    }

}
